/**   
 * Copyright © 2021 郑心怡. All rights reserved.
 * 
 * @Title: RoleController.java 
 * @Prject: com.interesting.examsystem.role.controller
 * @Package: com.interesting.examsystem.role.controller
 * @author: ZhengXinyi   
 * @date: 2021-07-25
 * @version: V1.0   
 */
package com.interesting.examsystem.role.controller;

import java.util.Arrays;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.interesting.examsystem.common.annotation.LogAnnotation;
import com.interesting.examsystem.common.constant.RestConstant;
import com.interesting.examsystem.common.core.controller.BaseController;
import com.interesting.examsystem.common.enums.OperateTypeEnum;
import com.interesting.examsystem.common.response.Result;
import com.interesting.examsystem.common.response.ResultGenerator;
import com.interesting.examsystem.role.model.RoleDTO;
import com.interesting.examsystem.role.model.RoleVO;
import com.interesting.examsystem.role.service.IRoleService;
import com.interesting.examsystem.common.utils.InputInjectFilter;

/**
 * @ClassName: RoleController
 * @Description:  控制层
 * @author: ZhengXinyi 
 * @date: 2021-07-25
 */
@RestController
@RequestMapping(RoleController.PATH)
@Api(tags = "系统->角色")
public class RoleController extends BaseController {
	public final static String PATH = RestConstant.VERSION_V1 + "/sys/role";
	// 日志审计列表中“菜单路径”
	private static final String MENU_NAME = "系统->角色";
	// 日志审计列表中“日志内容”显示信息
	private static final String CONTENT = "角色管理";
	@Autowired
	private IRoleService roleService;

	/**
	 * @Title: selectPage
	 * @Description: 分页查询
	 * @param roleDto
	 * @return
	 */
	@LogAnnotation(menuName = MENU_NAME, content = CONTENT, operateType = OperateTypeEnum.QUERY)
	@GetMapping
	@ApiOperation(value = "分页查询", httpMethod = "GET")
	@ApiImplicitParam(name = "roleDto", value = "查询参数", required = true)
	public Result selectPageVo(@Validated RoleDTO roleDto) {
		roleDto = (RoleDTO) InputInjectFilter.encodeInputString(roleDto,RoleDTO.class);
		Page<RoleDTO> page = new Page<>(roleDto.getPageNo(),roleDto.getPageSize());
		Page<RoleVO> result = roleService.selectPageVo(page, roleDto);
		return ResultGenerator.genOkResult(result);
	}	
	/**
	 * @Title: selectById
	 * @Description: 根据主键查询
	 * @param id
	 * @return
	 */
	@LogAnnotation(menuName = MENU_NAME, content = CONTENT, operateType = OperateTypeEnum.QUERY)
	@GetMapping("/{id}")
	@ApiOperation(value = "根据主键查询", httpMethod = "GET")
	@ApiImplicitParam(name = "id", value = "主键", required = true)
	public Result selectById(@PathVariable String id) {
		RoleVO roleVo = roleService.selectById(id);
		return ResultGenerator.genOkResult(roleVo);
	}
	/**
	 * @Title: deleteBatchIds
	 * @Description: 批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/{ids}")
	@LogAnnotation(menuName = MENU_NAME, content = CONTENT, operateType = OperateTypeEnum.DELETE)
	@ApiOperation(value = "批量删除", httpMethod = "GET")
	@ApiImplicitParam(name = "ids", value = "主键数组", required = true)
	public Result deleteBatchIds(@PathVariable("ids") String[] ids) {
		List<String> list = Arrays.asList(ids);
		int i = roleService.deleteBatchIds(list);
		return ResultGenerator.genOkResult(i);
	}
	/**
	 * @Title: insert
	 * @Description: 新增记录
	 * @param roleVo
	 * @return
	 */
	@PostMapping
	@LogAnnotation(menuName = MENU_NAME, content = CONTENT, operateType = OperateTypeEnum.INSERT)
	@ApiOperation(value = "新增记录", httpMethod = "POST")
	@ApiImplicitParam(name = "roleVo", value = "vo对象", required = true)
	public Result insert(@RequestBody @Validated RoleVO roleVo) {
		roleVo = (RoleVO) InputInjectFilter.encodeInputString(roleVo,RoleVO.class);
		roleVo.setOperatorId(this.getOperatorId());
		int i = roleService.insert(roleVo);
		return ResultGenerator.genOkResult(i);
	}
	/**
	 * @Title: update
	 * @Description: 修改记录
	 * @param roleVo
	 * @return
	 */
	@PutMapping
	@LogAnnotation(menuName = MENU_NAME, content = CONTENT, operateType = OperateTypeEnum.UPDATE)
	@ApiOperation(value = "修改记录", httpMethod = "PUT")
	@ApiImplicitParam(name = "roleVo", value = "vo对象", required = true)
	public Result update(@RequestBody @Validated RoleVO roleVo) {
		roleVo = (RoleVO) InputInjectFilter.encodeInputString(roleVo,RoleVO.class);
		int i = roleService.update(roleVo);
		return ResultGenerator.genOkResult(i);
	}

}
